import {Component, ElementRef} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {BaseAuthenticateComponent} from "../shared/component/baseAuthenticate.component";
import {User} from "../entity/User";
import {UserService} from "../service/userService";
import {JsSdkService} from "../service/jsSdkService";
import {WechatEntity} from "../entity/WechatEntity";
declare var wx: any;
declare var decode: any;
@Component({
    selector: 'app-login',
    templateUrl: 'appLogin.component.html'
})
export class AppLoginComponent {
    private jwtToken = "";
    private successURL = "/dashboard";

    user: User = new User();
    openid: string;
    wechatEntity: WechatEntity;

    constructor(protected a_router: Router, public element: ElementRef, public route: ActivatedRoute,
                private _userService: UserService) {
        // super(a_router, element, route, false);
        // this.route.params
        //     .map(params => {
        //         return {token: params.token, id: params.id, state: params.state};
        //     }).subscribe(
        //     (result) => {
        //         this.jwtToken = result.token;
        //         localStorage.setItem('token', result.token);
        //         this._userService.get(result.id).subscribe(
        //             user => {
        //                 localStorage.setItem('loginUser', JSON.stringify(user));
        //                 // TODO analyze state and do related actions
        //                 if (result.state === 'login') {
        //                   // 获取微信jsapi的调用权限，针对angular单页面应用 android和ios调用的签名方式不同
        //
        //                   if (!(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent))) {
        //                     window.location.href = '/dashboard/task/list';
        //                   } else {
        //                     this.a_router.navigate(['/dashboard/task/list']);
        //                   }
        //                 }
        //             },
        //             err => {
        //                 this.jwtToken = JSON.stringify(err);
        //             }
        //         );
        //     });
      this.route
        .queryParams
        .subscribe(params => {
          let encrypt = params['encrypt'];
          if (encrypt) {
            encrypt = encrypt.replace(/\s/g, "+");
            const wechatInfo = decode(encrypt);
            localStorage.setItem('wechatEntity', wechatInfo);
            if (wechatInfo) {
              this.wechatEntity = JSON.parse(wechatInfo);
              localStorage.setItem('openid', this.wechatEntity.openid);
              // console.log('::: openid ::: ' + this.wechatEntity.openid);
              this._userService.wechatLogin(this.wechatEntity.openid).subscribe(
                (resp) => {
                    // 已经存在绑定关系 => 进入系统
                    if (resp) {
                      console.log('login' + JSON.stringify(resp))
                      localStorage.setItem('token', resp.token);
                      this.user.userid = resp.userId;
                      localStorage.setItem('loginUser', JSON.stringify(this.user));
                      window.location.href = 'dashboard/task/list';
                    }
                },
                err => {
                  window.location.href = 'scan';
                }
              )
            }
          }

        });

    }
}
